package com.code.impl;

import com.code.entity.composite.Dependency;
import com.code.entity.composite.PomComponent;
import com.code.entity.composite.PomNodeItem;
import com.code.entity.composite.RelativePath;
import com.code.impl.base.BaseCodeWrite;
import com.common.config.ProjectConfig;

public class PomCodeWrite extends BaseCodeWrite {

    private StringBuilder builder = new StringBuilder();

    public PomCodeWrite(ProjectConfig projectConfig) {
        super(projectConfig);


        builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        builder.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        builder.append("         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd\">");
        builder.append("<modelVersion>4.0.0</modelVersion>");
        builder.append("<parent>");
        builder.append("<groupId>org.springframework.boot</groupId>");
        builder.append("<artifactId>spring-boot-starter-parent</artifactId>");
        builder.append("<version>2.3.3.RELEASE</version>");
        builder.append("<relativePath/>");
        builder.append("</parent>");
        builder.append("<groupId>").append(projectConfig.getProjectName()).append("</groupId>");
        builder.append("<artifactId>").append(projectConfig.getProjectName()).append("</artifactId>");
        builder.append("<version>0.0.1-SNAPSHOT</version>");
        builder.append("<packaging>").append("jar").append("</packaging>");
        builder.append("<description>").append(projectConfig.getDescription()).append("</description>");
        builder.append("<properties>");
        builder.append("<maven.compiler.source>11</maven.compiler.source>");
        builder.append("<maven.compiler.target>11</maven.compiler.target>");
        builder.append("<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>");
        builder.append("<mysql.version>5.1.47</mysql.version>");
        builder.append("<org.apache.commons.version>3.12.0</org.apache.commons.version>");
        builder.append("<org.springframework.boot.version>2.7.2</org.springframework.boot.version>");
        builder.append("<commons.io.version>2.11.0</commons.io.version>");
        builder.append("</properties>");
        builder.append("<dependencies>");
        this.start();
        builder.append("</dependencies>");
        this.end();
        builder.append("</project>");


    }

    private void start() {

    }

    private void end() {
        builder.append("<build>\n" +
                "        <finalName>ROOT</finalName>\n" +
                "        <plugins>\n" +
                "            <plugin>\n" +
                "                <groupId>org.springframework.boot</groupId>\n" +
                "                <artifactId>spring-boot-maven-plugin</artifactId>\n" +
                "            </plugin>\n" +
                "            <!-- 下面是为了将本地jar包打入WEB-INF/lib下而增加的配置-->\n" +
                "            <plugin>\n" +
                "                <groupId>org.apache.maven.plugins</groupId>\n" +
                "                <artifactId>maven-war-plugin</artifactId>\n" +
                "                <version>3.2.2</version>\n" +
                "                <configuration>\n" +
                "                    <webResources>\n" +
                "                        <resource>\n" +
                "                            <!-- 指向的是包含你所有要用jar包的目录 -->\n" +
                "                            <directory>${pom.basedir}/src/lib</directory>\n" +
                "                            <!-- 编译后要把这些jar包复制到的位置 -->\n" +
                "                            <targetPath>WEB-INF/lib</targetPath>\n" +
                "                        </resource>\n" +
                "                    </webResources>\n" +
                "                </configuration>\n" +
                "            </plugin>\n" +
                "        </plugins>\n" +
                "        <resources>\n" +
                "            <resource>\n" +
                "                <directory>src/main</directory>\n" +
                "                <includes>\n" +
                "                    <!-- **表示任意目录,*.*表示任意文件名和扩展名-->\n" +
                "                    <include>**/*.*</include>\n" +
                "                </includes>\n" +
                "                <!-- 表示是否对配置文件中的${}占位符进行解析替换-->\n" +
                "                <filtering>false</filtering>\n" +
                "            </resource>\n" +
                "            <resource>\n" +
                "                <directory>src/main/resources</directory>\n" +
                "                <includes>\n" +
                "                    <include>**/*.*</include>\n" +
                "                </includes>\n" +
                "            </resource>\n" +
                "            <resource>\n" +
                "                <directory>src/main/java</directory>\n" +
                "                <includes>\n" +
                "                    <include>**/*.*</include>\n" +
                "                </includes>\n" +
                "            </resource>\n" +
                "\n" +
                "        </resources>\n" +
                "    </build>");
    }

    public static void main(String[] args) {
        ProjectConfig projectConfig = ProjectConfig.ProjectConfigBuilder.aProjectConfig().projectDescription("测试").projectName("server").build();


        PomComponent project = new PomComponent("project", "xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
                "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"");

        PomNodeItem modelVersion = new PomNodeItem("modelVersion", "4.0.0");
        project.add(modelVersion);

        PomComponent parent = new PomComponent("parent", "");
        Dependency boot = new Dependency("org.springframework.boot", "spring-boot-starter-parent", "2.3.3.RELEASE");
        parent.add(boot);
        RelativePath relativePath = new RelativePath("relativePath");
        parent.add(relativePath);
        project.add(parent);

        Dependency arg = new Dependency(projectConfig.getProjectName(), projectConfig.getProjectName(), "0.0.1-SNAPSHOT");
        project.add(arg);

        PomNodeItem packing = new PomNodeItem("packaging", "jar");
        project.add(packing);

        PomNodeItem description = new PomNodeItem("description", projectConfig.getDescription());
        project.add(description);

        PomComponent properties = new PomComponent("properties", "");
        project.add(properties);

        PomNodeItem source = new PomNodeItem("maven.compiler.source", "11");
        properties.add(source);

        PomNodeItem target = new PomNodeItem("maven.compiler.target", "11");
        properties.add(target);

        PomNodeItem sourceEncoding = new PomNodeItem("project.build.sourceEncoding", "UTF-8");
        properties.add(sourceEncoding);

        PomNodeItem mysql = new PomNodeItem("mysql.version", "5.1.47");
        properties.add(mysql);


        String build = project.build("");
        System.out.println(build);
    }
}
